<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="JavaScript，DOM">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day16">
<meta property="og:url" content="http://yoursite.com/2020/05/13/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day16/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="JavaScript，DOM">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-13T07:16:18.000Z">
<meta property="article:modified_time" content="2020-05-17T15:42:14.479Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/05/13/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day16/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day16 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/05/13/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day16/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day16
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-13 15:16:18" itemprop="dateCreated datePublished" datetime="2020-05-13T15:16:18+08:00">2020-05-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-17 23:42:14" itemprop="dateModified" datetime="2020-05-17T23:42:14+08:00">2020-05-17</time>
              </span>

          
            <div class="post-description">JavaScript，DOM</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><code>JavaScript</code>语言诞生主要是完成页面上的数据验证。因此它运行在客户端，需要运行浏览器来解析<code>JavaScript</code>代码</p>
<p>特点：</p>
<ol>
<li>交互性</li>
<li>安全性（不允许直接访问本地硬盘）</li>
<li>跨平台性</li>
</ol>
<h2 id="JavaScript和html代码的结合方式"><a href="#JavaScript和html代码的结合方式" class="headerlink" title="JavaScript和html代码的结合方式"></a>JavaScript和html代码的结合方式</h2><h3 id="直接在script标签内部书写js代码"><a href="#直接在script标签内部书写js代码" class="headerlink" title="直接在script标签内部书写js代码"></a>直接在script标签内部书写js代码</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 在script标签内部可以书写JS注释和JS代码 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">  <span class="comment">//JS的单行注释</span></span></span><br><span class="line"><span class="actionscript">  <span class="comment">/* JS的多行注释 */</span></span></span><br><span class="line"><span class="actionscript">    alert( <span class="string">"在html中引入JS的第一种方式..."</span> );</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="通过script标签引入外部js文件"><a href="#通过script标签引入外部js文件" class="headerlink" title="通过script标签引入外部js文件"></a>通过script标签引入外部js文件</h3><p>在head或body标签内部，可以通过script标签引入外部的JS文件。例如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 通过script标签可以引入外部的JS文件 </span></span><br><span class="line"><span class="comment">src属性专门用来引入js问价路径（可以是相对路径也可以是绝对路径）</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"demo.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><p>JavaScript的变量类型：</p>
<ul>
<li>数值类型：number</li>
<li>字符串类型：string</li>
<li>对象类型：object</li>
<li>布尔类型：boolean</li>
<li>函数类型：function</li>
</ul>
<p>JavaScript中的特殊值</p>
<ul>
<li>undefined：未定义，所有JavaScript变量未赋值时，默认值都是undefined</li>
<li>null：空值</li>
<li>NANA：Not a Number，非数字，非数值</li>
</ul>
<p>JavaScript中定义变量格式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> 变量名;</span><br><span class="line"><span class="keyword">var</span> 变量名 = 值;</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">var</span> i;</span></span><br><span class="line"><span class="actionscript">        <span class="comment">// alert(i); // undefined</span></span></span><br><span class="line">        i = 12;</span><br><span class="line"><span class="actionscript">        <span class="comment">// typeof()是 JavaScript 语言提供的一个函数。</span></span></span><br><span class="line"><span class="actionscript">        <span class="comment">// alert( typeof(i) ); // numberi = "abc";</span></span></span><br><span class="line"><span class="actionscript">        <span class="comment">// 它可以取变量的数据类型返回</span></span></span><br><span class="line"><span class="actionscript">        <span class="comment">// alert( typeof(i) ); // String</span></span></span><br><span class="line"><span class="actionscript">        <span class="keyword">var</span> a = <span class="number">12</span>;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">var</span> b = <span class="string">"abc"</span>;</span></span><br><span class="line"><span class="actionscript">        alert( a * b ); <span class="comment">// NaN 是非数字， 非数值。</span></span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="关系运算"><a href="#关系运算" class="headerlink" title="关系运算"></a>关系运算</h2><ul>
<li>等于：<code>==</code>简单的字面值比较</li>
<li>全等于：<code>===</code>比较值与数据类型</li>
</ul>
<p>例如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">var</span> a = <span class="string">"12"</span>;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">var</span> b = <span class="number">12</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript">        alert( a == b ); <span class="comment">// true</span></span></span><br><span class="line"><span class="actionscript">        alert( a === b ); <span class="comment">// false</span></span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="逻辑运算"><a href="#逻辑运算" class="headerlink" title="逻辑运算"></a>逻辑运算</h2><ul>
<li>且：<code>&amp;&amp;</code></li>
<li>或：<code>||</code></li>
<li>取反：<code>!</code></li>
</ul>
<p>在JavaScript中，所有的变量都可以作为<code>boolean</code>类型的变量使用。且<code>0</code>、<code>null</code>、<code>undefined</code>、<code>&quot;&quot;</code>（空串）都认为是<code>false</code></p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line">    /* 在 JavaScript 语言中， 所有的变量， 都可以做为一个 boolean 类型的变量去使用。</span><br><span class="line"><span class="actionscript">    <span class="number">0</span> 、 <span class="literal">null</span>、 <span class="literal">undefined</span>、 ””(空串) 都认为是 <span class="literal">false</span>； */</span></span><br><span class="line"><span class="actionscript">    <span class="comment">// var a = 0;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// if (a) &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("零为真");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125; else &#123;// alert("零为假");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// var b = null;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// if (b) &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("null 为真");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125; else &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("null 为假");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// var c = undefined;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// if (c) &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("undefined 为真");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125; else &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("undefined 为假");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// var d = "";</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// if (d) &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("空串为真");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125; else &#123;</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert("空串为假");</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// &#125;</span></span></span><br><span class="line">    /* &amp;&amp; 且运算。</span><br><span class="line">    有两种情况：</span><br><span class="line">    第一种： 当表达式全为真的时候。 返回最后一个表达式的值。</span><br><span class="line">    第二种： 当表达式中， 有一个为假的时候。 返回第一个为假的表达式的值*/</span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> a = <span class="string">"abc"</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> b = <span class="literal">true</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> d = <span class="literal">false</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> c = <span class="literal">null</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( a &amp;&amp; b );//true</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( b &amp;&amp; a );//true</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( a &amp;&amp; d ); // false</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( a &amp;&amp; c ); // null</span></span></span><br><span class="line">    /* || 或运算</span><br><span class="line">    第一种情况： 当表达式全为假时， 返回最后一个表达式的值</span><br><span class="line">    第二种情况： 只要有一个表达式为真。 就会把回第一个为真的表达式的值*/</span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( d || c ); // null</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( c|| d ); //false</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( a || c ); //abc</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( b || c ); //true</span></span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><p>格式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> 数组名 = []; <span class="comment">//空数组</span></span><br><span class="line"><span class="keyword">var</span> 数组名 = [<span class="number">1</span>,<span class="string">'abc'</span>,<span class="literal">true</span>]; <span class="comment">//定义数组并赋值</span></span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> arr = [ <span class="literal">true</span>, <span class="number">1</span> ]; <span class="comment">// 定义一个数组</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( arr.length ); // 2</span></span></span><br><span class="line">    arr[0] = 12;</span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( arr[0] );//12</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert( arr.length ); // 0</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// javaScript 语言中的数组， 只要我们通过数组下标赋值， 那么最大的下标值， 就会自动的给数组做扩容操作。</span></span></span><br><span class="line"><span class="actionscript">    arr[<span class="number">2</span>] = <span class="string">"abc"</span>;</span></span><br><span class="line"><span class="actionscript">    alert(arr.length); <span class="comment">//3</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// alert(arr[1]);// undefined</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// 数组的遍历</span></span></span><br><span class="line"><span class="actionscript">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span></span><br><span class="line">        alert(arr[i]);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>两个细节：</p>
<ol>
<li>JavaScript的数组可以存储任意类型的数据</li>
<li>JavaScript的数组长度可以被任意改变</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [];</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"此处数组的长度为: "</span>+arr1.length ); <span class="comment">// 0</span></span><br><span class="line">arr1.length = <span class="number">5</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"此处数组的长度为: "</span>+arr1.length ); <span class="comment">// 5</span></span><br><span class="line">arr1[<span class="number">9</span>] = <span class="string">"a"</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"此处数组的长度为: "</span>+arr1.length ); <span class="comment">// 10</span></span><br><span class="line">arr1[<span class="number">99</span>] = <span class="string">"b"</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"此处数组的长度为: "</span>+arr1.length ); <span class="comment">// 100</span></span><br></pre></td></tr></table></figure>

<h2 id="JavaScript函数"><a href="#JavaScript函数" class="headerlink" title="JavaScript函数"></a>JavaScript函数</h2><h3 id="使用function关键字来定义函数"><a href="#使用function关键字来定义函数" class="headerlink" title="使用function关键字来定义函数"></a>使用<code>function</code>关键字来定义函数</h3><p>格式如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> 函数名(<span class="params">形参列表</span>)</span>&#123;</span><br><span class="line">    函数体</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>或者</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> 变量名/函数名 = <span class="function"><span class="keyword">function</span>(<span class="params">[参数列表]</span>)</span>&#123;</span><br><span class="line">    函数体</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果要定义带有返回值的函数，只需要在函数体内直接使用<code>return</code>语句返回值即可</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">&lt;title&gt;Title&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">&lt;script type="text/</span>javascript<span class="string">"&gt;</span></span><br><span class="line"><span class="string">    // 定义一个无参函数</span></span><br><span class="line"><span class="string">    function fun() &#123;</span></span><br><span class="line"><span class="string">        alert("</span>无参函数 fun()被调用了<span class="string">");</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    // 函数调用 === 才会执行</span></span><br><span class="line"><span class="string">    // fun();</span></span><br><span class="line"><span class="string">    function fun2(a, b) &#123;</span></span><br><span class="line"><span class="string">        alert("</span>有参函数 fun2()被调用了 a=&gt;<span class="string">" + a + "</span>,b=&gt;<span class="string">" + b);</span></span><br><span class="line"><span class="string">    &#125; // fun2(12, "</span>abc<span class="string">");</span></span><br><span class="line"><span class="string">    // 定义带有返回值的函数</span></span><br><span class="line"><span class="string">    function sum(num1, num2) &#123;</span></span><br><span class="line"><span class="string">        var result = num1 + num2;</span></span><br><span class="line"><span class="string">        return result;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    alert(sum(100, 50));</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="综合练习"><a href="#综合练习" class="headerlink" title="综合练习"></a>综合练习</h3><p>声明一个函数fn，在函数中实现：遍历指定的两个数值(例如1和100)之间的整数，将是3的倍数的数值存入到一个数组中，并将数组返回。</p>
<ul>
<li><ol>
<li>声明fn函数</li>
</ol>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">x,y</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = x,j = <span class="number">0</span>;i&lt;j;i++&gt;)&#123;</span><br><span class="line">        <span class="keyword">if</span>(i%<span class="number">3</span> == <span class="number">0</span>)&#123;</span><br><span class="line">            arr[j] = i;</span><br><span class="line">            j++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="2">
<li>调用fn函数，获取1~100之间是3的倍数的数值组成的数组</li>
</ol>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = fn(<span class="number">1</span>,<span class="number">100</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="3">
<li>遍历数组中的元素，输出在网页上（使用<code>document.write(&quot;输出的内容&quot;)</code>）</li>
</ol>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=o;i&lt;arr.length;i++)&#123;</span><br><span class="line">    <span class="built_in">document</span>.write(arr[i] + <span class="string">" "</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="DOM模型"><a href="#DOM模型" class="headerlink" title="DOM模型"></a>DOM模型</h2><p>DOM是一种文档对象模型，同时也是用于html编程的接口，通过DOM来操作页面中的元素。当HTML页面被加载的时候，浏览器会创建一个DOM，给文档提供了一种新的逻辑结构，并且可以改变内容和结构。</p>
<p>DOM是面向对象，并且定义了修改问的那个所需要的对象，各个对象之间的关系，也可以把DOM看成一个树状结构，通过JavaScript，对HTML文档进行添加排版，JavaScript想要访问HTML中的元素，就要通过对象模型来获得。</p>
<p>DOM的核心对象就是<code>document</code>对象，<code>document</code>对象是浏览器内置的一个对象，存储这专门用来操作元素的方法</p>
<h3 id="Document对象中的方法"><a href="#Document对象中的方法" class="headerlink" title="Document对象中的方法"></a><code>Document</code>对象中的方法</h3><ul>
<li><code>document.getElementById(elementId)</code>：通过标签的id属性查找标签DOM对象</li>
<li><code>document.getElementByName(elementName</code>：通过标签的name属性查找标签DOM对象</li>
<li><code>document.getElementByTagName(tagname)</code>：通过标签名查找DOM对象</li>
<li><code>document.createElement(tagName)</code>：通过给定的标签名，创建一个标签对象</li>
</ul>
<p><code>document</code>对象的三个查询方法，如果有id属性，优先使用<code>getElementById</code>方法来进行查询，如果没有id属性，则使用<code>getElementByName</code>方法，如果id和name都没有，再使用<code>getElementByTagName</code></p>
<p>上述三个方法，一定要在页面加载完成之后再执行，才能查询到标签对象</p>
<h3 id="案例：电灯开关"><a href="#案例：电灯开关" class="headerlink" title="案例：电灯开关"></a>案例：电灯开关</h3><p>点击电灯可以实现开/关灯，代码实现如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> flag = <span class="string">"off"</span>; <span class="comment">//flag表示灯的状态, off表示灯是关闭的!</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">changeImg</span>(<span class="params"> </span>)</span>&#123;</span><br><span class="line">    <span class="comment">//1.通过id获取img元素(返回是一个JS对象)</span></span><br><span class="line">    <span class="keyword">var</span> imgObj = <span class="built_in">document</span>.getElementById(<span class="string">"img1"</span>);</span><br><span class="line">    <span class="keyword">if</span>( flag == <span class="string">"off"</span> )&#123; <span class="comment">// 表明灯是关闭状态, 点击后则需要打开</span></span><br><span class="line">        imgObj.src = <span class="string">"imgs/on.gif"</span>;</span><br><span class="line">        flag = <span class="string">"on"</span>; <span class="comment">// 更新灯的状态为开灯</span></span><br><span class="line">    &#125;<span class="keyword">else</span>&#123; <span class="comment">// flag="on" 表名灯是打开状态, 点击后则需要关闭</span></span><br><span class="line">        imgObj.src = <span class="string">"imgs/off.gif"</span>;</span><br><span class="line">        flag = <span class="string">"off"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/07/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day15/" rel="prev" title="Java日拱一卒-day15">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day15
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/15/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day17/" rel="next" title="Java日拱一卒-day17">
      Java日拱一卒-day17 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript和html代码的结合方式"><span class="nav-number">1.</span> <span class="nav-text">JavaScript和html代码的结合方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#直接在script标签内部书写js代码"><span class="nav-number">1.1.</span> <span class="nav-text">直接在script标签内部书写js代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过script标签引入外部js文件"><span class="nav-number">1.2.</span> <span class="nav-text">通过script标签引入外部js文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量"><span class="nav-number">2.</span> <span class="nav-text">变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关系运算"><span class="nav-number">3.</span> <span class="nav-text">关系运算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逻辑运算"><span class="nav-number">4.</span> <span class="nav-text">逻辑运算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组"><span class="nav-number">5.</span> <span class="nav-text">数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript函数"><span class="nav-number">6.</span> <span class="nav-text">JavaScript函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用function关键字来定义函数"><span class="nav-number">6.1.</span> <span class="nav-text">使用function关键字来定义函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#综合练习"><span class="nav-number">6.2.</span> <span class="nav-text">综合练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DOM模型"><span class="nav-number">7.</span> <span class="nav-text">DOM模型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Document对象中的方法"><span class="nav-number">7.1.</span> <span class="nav-text">Document对象中的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#案例：电灯开关"><span class="nav-number">7.2.</span> <span class="nav-text">案例：电灯开关</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
